package com.zjhome.cloudnote.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjhome.cloudnote.constant.ErrorCode;
import com.zjhome.cloudnote.domain.DirInfo;
import com.zjhome.cloudnote.domain.NoteInfo;
import com.zjhome.cloudnote.exception.BaseException;
import com.zjhome.cloudnote.param.AckResult;
import com.zjhome.cloudnote.service.DirService;
import com.zjhome.cloudnote.service.NoteService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 笔记
 * 
 * @author zjhome
 *
 */
@RestController
@Api(value = "笔记管理")
@RequestMapping("/notemgr")
public class NoteController extends BaseController {
	
	@Autowired
	private NoteService		noteService;
	
	
	/**
	 * 文件夹列表查询
	 * 
	 * @param did	父文件夹id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/list")
	@ApiOperation(value = "笔记列表")
	public List<NoteInfo> getDirs( @RequestParam(value = "did", required = false) String did ) throws Exception {
		String uid = this.getUserId();
		List<NoteInfo> noteList = noteService.selectSubNotesByDid(did, uid);
		
		return noteList;
	}
	
	/**
	 * 获取笔记信息
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "获取文件夹信息")
	@GetMapping(value = "/note")
	public NoteInfo getNoteInfo(String id) throws Exception {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException("", ErrorCode.ERR_INVALID_ID);
		}		
		
		String uid = this.getUserId();
		NoteInfo noteInfo = noteService.selectNoteById(id, uid);
		
		return noteInfo;		
	}

	/**
	 * 创建笔记
	 * 
	 * @param noteInfo	笔记信息
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/createnote")
	public AckResult createNote(NoteInfo noteInfo) throws Exception {		
		noteInfo.setUid(this.getUserId());
		noteService.insertNoteByParam(noteInfo);
		return new AckResult(ErrorCode.ERR_NORMAL);
	}	
	
	/**
	 * 更新笔记信息 
	 * 
	 * @param noteInfo
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/updatenote")
	public AckResult updateNote(NoteInfo noteInfo) throws Exception {
		
		if (StringUtils.isEmpty(noteInfo.getId())) {
			throw new BaseException("", ErrorCode.ERR_INVALID_ID);
		}
		
		noteInfo.setUid(this.getUserId());		
		noteService.updateNoteByParam(noteInfo);
		
		return new AckResult(ErrorCode.ERR_NORMAL);
	}
	
		
	/**
	 * 删除笔记
	 * 
	 * @param id	笔记id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/delnote")
	public AckResult delNote(String id) throws Exception {
		
		if (StringUtils.isEmpty(id)) {
			throw new BaseException("", ErrorCode.ERR_INVALID_ID);
		}
		
		noteService.deleteNoteById(id, this.getUserId());
		
		return new AckResult(ErrorCode.ERR_NORMAL);
	}
	
}
